package com.bxs.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Date;

@Component
public class MyMetaObjectHandler implements MetaObjectHandler {


    @Override
    public void insertFill(MetaObject metaObject) {
//        会对字段进行判空处理，如果createTime为null时才会插入
//        this.setFieldValByName("createTime",new Date(),metaObject);
//        强制插入，不会判空
//        System.out.println("insert field");
//        this.strictInsertFill(metaObject, "created_time", LocalDateTime.class,LocalDateTime.now()); // 起始版本 3.3.0(推荐使用)
//        this.strictUpdateFill(metaObject, "updated_time", LocalDateTime.class, LocalDateTime.now());
        this.setFieldValByName("createdTime", new Date(), metaObject);
        this.setFieldValByName("updatedTime", new Date(), metaObject);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        System.out.println("update field");
//        this.strictUpdateFill(metaObject, "updated_time", LocalDateTime.class, LocalDateTime.now()); // 起始版本 3.3.0(推荐)
        this.setFieldValByName("updatedTime", new Date(), metaObject);
    }
}